package cn.edu.zafu.bigdata.yeshuchang.xsglxt;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class StudentManager {
    private List<Student> students;
    private static final String FILE_NAME = "students.ser";

    public StudentManager() {
        students = new ArrayList<>();
        loadStudents();
    }

    public void addStudent(Student student) {
        if (students.stream().anyMatch(s -> s.getId().equals(student.getId()))) {
            System.out.println("学号重复，请重新输入。");
        } else {
            students.add(student);
            saveStudents();
        }
    }

    public void retrieveStudent() {
        while (true) {
            System.out.print("输入学号检索学生信息（输入'QUIT'退出）: ");
            String id = readLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Optional<Student> studentOptional = students.stream().filter(s -> s.getId().equals(id)).findFirst();
            studentOptional.ifPresentOrElse(
                    student -> System.out.println(student),
                    () -> System.out.println("未找到该学生。")
            );
        }
    }

    public void updateStudent() {
        while (true) {
            System.out.print("输入学号更新学生信息（输入'QUIT'退出）: ");
            String id = readLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Optional<Student> studentOptional = students.stream().filter(s -> s.getId().equals(id)).findFirst();
            studentOptional.ifPresentOrElse(
                    student -> {
                        System.out.print("输入新的姓名: ");
                        String name = readLine();
                        System.out.print("输入新的电话: ");
                        String phone = readLine();
                        student.setName(name);
                        student.setPhone(phone);
                        saveStudents();
                        System.out.println("学生信息更新成功！");
                    },
                    () -> System.out.println("未找到该学生。")
            );
        }
    }

    public void deleteStudent() {
        while (true) {
            System.out.print("输入学号删除学生信息（输入'QUIT'退出）: ");
            String id = readLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            boolean removed = students.removeIf(s -> s.getId().equals(id));
            if (removed) {
                saveStudents();
                System.out.println("学生信息删除成功！");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void loadStudents() {
        File file = new File(FILE_NAME);
        if (file.exists()) {
            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
                students = (List<Student>) ois.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    private String readLine() {
        try {
            return new BufferedReader(new InputStreamReader(System.in)).readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}